package com.ken.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ken.constant.StatusConstant;
import com.ken.context.BaseContext;
import com.ken.dto.DishDTO;
import com.ken.entity.Dish;
import com.ken.entity.DishFlavor;
import com.ken.mapper.DishFlavorMapper;
import com.ken.mapper.DishMapper;
import com.ken.mapper.MealDishMapper;
import com.ken.mapper.SetmealMapper;
import com.ken.service.DishFlavorService;
import com.ken.service.DishService;
import com.ken.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private MealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


    /**
     * 新增菜品
     * @param dto
     */
    @Override
    @Transactional
    public void insertDishAndFlavor(DishDTO dto) {
        Long currentId = BaseContext.getCurrentId();
        // 1. 将DTO转换为Dish实体
        Dish dish = BeanUtil.copyProperties(dto, Dish.class);
        //基本信息
        dish.setStatus(StatusConstant.ENABLE);
        dish.setCreateUser(currentId);
        dish.setUpdateUser(currentId);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        // 2. 保存菜品基本信息
        save(dish);
        // 3. 获取菜品ID
        Long dishId = dish.getId();
        // 4. 获取口味列表
        List<DishFlavor> flavors = dto.getFlavors();
        // 5. 为每个口味设置菜品ID并封装为DishFlavor对象
        if (flavors != null && !flavors.isEmpty()) {
            List<DishFlavor> dishFlavors = new ArrayList<>();
            for (DishFlavor flavor : flavors) {
                // 创建新的DishFlavor对象并设置属性
                DishFlavor dishFlavor = DishFlavor.builder()
                        .dishId(dishId)
                        .name(flavor.getName())
                        .value(flavor.getValue())
                        .build();
                dishFlavors.add(dishFlavor);
            }
            // 6. 批量保存口味数据
            dishFlavorService.saveBatch(dishFlavors);
        }
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteById(String ids) {
        List<Integer> collect = Arrays.stream(ids.split(",")).map(Integer::valueOf).collect(Collectors.toList());
        removeBatchByIds(collect);
        dishFlavorService.remove(
                new LambdaQueryWrapper<DishFlavor>()
                        .in(DishFlavor::getDishId,collect));
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void updateDishAndFlavor(DishDTO dishDTO) {
        Long currentId = BaseContext.getCurrentId();
        // 1. 更新菜品基本信息
        Dish dish = BeanUtil.copyProperties(dishDTO, Dish.class);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(currentId);
        updateById(dish);
        // 2. 获取菜品ID
        Long dishId = dish.getId();
        // 3. 先删除原有的口味数据
        dishFlavorService.remove(
            new LambdaQueryWrapper<DishFlavor>()
                .eq(DishFlavor::getDishId, dishId)
        );
        // 4. 获取新的口味列表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        // 5. 为每个口味设置菜品ID并保存
        if (flavors != null && !flavors.isEmpty()) {
            List<DishFlavor> dishFlavors = new ArrayList<>();
            for (DishFlavor flavor : flavors) {
                // 创建新的DishFlavor对象并设置属性
                DishFlavor dishFlavor = DishFlavor.builder()
                        .dishId(dishId)
                        .name(flavor.getName())
                        .value(flavor.getValue())
                        .build();
                dishFlavors.add(dishFlavor);
            }
            // 6. 批量保存新的口味数据
            dishFlavorService.saveBatch(dishFlavors);
        }
    }
}
